package presenter;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Al
 */
public class VideoFrame extends JFrame implements ActionListener, WindowListener{
    JPanel panel;
    VideoPanel vp;
    Image background;
    Timer time;
    
    public VideoFrame(int x, int y) {
        vp=new VideoPanel(x,y);
        
        
    }
    
    public void updateBackground() {
        if(vp.getVideo()!=null&&vp.getVideo().getWidth()>0&&vp.getVideo().getHeight()>0) {
            BufferedImage bi=new BufferedImage(vp.getVideo().getWidth(),vp.getVideo().getHeight(),BufferedImage.TYPE_INT_RGB);
            Graphics gr=bi.getGraphics();
            vp.getVideo().paintAll(gr);
            background=(Image) bi;
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==time&&vp.getPlayer()!=null&&vp.getPlayer().getState()==Player.Started) {
            updateBackground();
        }
    }
    
    public Image getFrame() {
        return background;
    }
    
    public void loadVideo(String video) {
        String name=new File(video).getName();
        name=name.substring(0, name.lastIndexOf("."));
        super.setTitle(name);
        panel=new JPanel(new BorderLayout());
        vp.loadVideo(video);
        panel.add(vp,BorderLayout.CENTER);
        panel.add(vp.getVideoControls(),BorderLayout.SOUTH);
        setContentPane(panel);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        addWindowListener(this);
        setVisible(true);
        time=new Timer(40, this);
        time.start();
    }
    
    public void setSize(int x, int y) {
        vp.setSize(x,y);
        pack();
    }
    
    public static void main (String[] args) {
        new VideoFrame(800, 600).loadVideo("../ImaginationLand2.avi");
    }

    public void windowOpened(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowClosing(WindowEvent e) {
        vp.getPlayer().stop();
        vp.getPlayer().close();
    }

    public void windowClosed(WindowEvent e) {
       
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowIconified(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }
}
